package com.chinasoft.ui;

import com.chinasoft.enumdef.Direction;
import com.chinasoft.enumdef.Elem;
import com.chinasoft.obj.Man;
import com.chinasoft.obj.MapData;
import lombok.Data;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

@Data
public class GamePanel extends JPanel implements KeyListener {

    List<Image> bases = new ArrayList<>();
    MapData mapData;

    public GamePanel() {
        // 执行 数组的初始化
        this.initBases();
        this.setBackground(Color.gray);
        this.setBounds(0, 0, 600, 600);
        this.setVisible(true);
        setFocusable(true);
        // 注册监听器
        addKeyListener(this);
        requestFocus(true);
        this.mapData = new MapData();
        repaint();
    }

    public void initBases() {
        for(int i = 0; i < 10; i++) {
            Image im = Toolkit.getDefaultToolkit().getImage(
                            GamePanel.class.getClassLoader().getResource("image/scene/" + i +
//                    GamePanel.class.getResource("/image/scene/" + i +
                            ".GIF"));
            bases.add(im);
        }
    }

    @Override
    public void paint(Graphics g) {
        Elem[][] map = mapData.getMap();
        // 画草地
        for(int y = 0; y < mapData.WIDTH; y++) {
            for(int x = 0; x < mapData.WIDTH; x++) {
//                System.out.println("y = " + y + " x= " + x);
                Elem elem = map[y][x];
                int id = elem.getVal();

                switch(id) {
                    case 4:
                        g.drawImage(bases.get(0), x*30, y*30, 30, 30, this );
                        g.drawImage(bases.get(id), x*30, y*30, 30, 30, this );
                        break;
                    // 画人
                    case 5:
                        // 画草地
                        g.drawImage(bases.get(0), x*30, y*30, 30, 30, this );
                        // 画人
                        int dir = mapData.getMan().getManDir().getVal();
                        g.drawImage(bases.get(dir), x*30, y*30, 30, 30, this );
                        break;
                        // 其他
                    default:
                        // 画其他东西
                        g.drawImage(bases.get(id), x*30, y*30, 30, 30, this );
                        break;
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // null
    }

    @Override
    public void keyPressed(KeyEvent e) {

        Man man = mapData.getMan();
//        System.out.println("code is " + e.getKeyCode());
        switch(e.getKeyCode()) {
            case KeyEvent.VK_UP:
                man.dealMove(Direction.Up);
                repaint();
                break;
            case KeyEvent.VK_DOWN:
                man.dealMove(Direction.Down);
                repaint();
                break;
            case KeyEvent.VK_LEFT:
                man.dealMove(Direction.Left);
                repaint();
                break;
            case KeyEvent.VK_RIGHT:
                man.dealMove(Direction.Right);
                repaint();
                break;
            default:
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (mapData.isWin() == true) {
            if (mapData.getLevel() == mapData.MAX_LEVEL) {
                JOptionPane.showMessageDialog(this, " 已经是最后一关了 ！");
            } else {
                String msg = "是否要进入下一关？";
                UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("宋体", Font.ITALIC, 15)));
                int confirm = JOptionPane.showConfirmDialog(this, msg, "过关", JOptionPane.YES_NO_OPTION);
                if (confirm == JOptionPane.YES_OPTION) {
                    drawNextLevel();
                    mapData.saveCurrent();
                } else {
                    // null
                    return;
                }
            }
        }
    }

    /**
     * 画下一关
     *
     */
    public boolean drawNextLevel() {
        if (mapData.nextLevel() == false) {
            return false;
        } else {
            repaint();
            return true;
        }
    }

    public boolean drawGotoLevel(int level) {
        if (mapData.gotoLevel(level) == false) {
            return false;
        } else {
            repaint();
            return true;
        }
    }

    public boolean drawLoadLevel() {
        if (mapData.loadLevel() == false) {
            return false;
        } else {
            repaint();
            return true;
        }
    }
}
